<?php
/**
 * Svick a Fast Simple Smart PHP FrameWork
 * Author: Tommy 863758705@qq.com
 * Link: http://svick.tomener.com/
 * Since: 2022
 */

namespace Svick\Http\Client;


use GuzzleHttp\Promise\PromiseInterface;
use Psr\Http\Message\ResponseInterface;

class Promise implements PromiseInterface
{
    /**
     * @var \GuzzleHttp\Promise\PromiseInterface
     */
    protected $promise;

    public function __construct($promise)
    {
        $this->promise = $promise;
    }

    public function then(callable $onFulfilled = null, callable $onRejected = null)
    {
        $this->promise->then(
            function (ResponseInterface $res) use ($onFulfilled) {
                $onFulfilled(new Response($res));
            },
            function (\GuzzleHttp\Exception\RequestException $exception) use ($onRejected) {
                $onRejected(new RequestException($exception));
            }
        );
    }

    public function otherwise(callable $onRejected)
    {
        $this->promise->otherwise($onRejected);
    }

    public function getState()
    {
        $this->promise->getState();
    }

    public function resolve($value)
    {
        $this->promise->resolve($value);
    }

    public function reject($reason)
    {
        $this->promise->reject($reason);
    }

    public function cancel()
    {
        $this->promise->cancel();
    }

    public function wait($unwrap = true)
    {
        $this->promise->wait($unwrap);
    }
}
